const { UI_Layer } = require("../../config/ui_def");
const { Sdk_Platform, WX_AD_CONFIG } = require("../../frame/constants");
const { getLongScreen } = require("../tool/util");


cc.Class({
    extends: cc.Component,

    properties: {
        role_sp: cc.Sprite,
        colse_node: cc.Node,
        bottom: cc.Node,
        close_btn: cc.Button,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {
    },

    updatePage(param) {
        this.isShowAd = false
        gg.data.isTrail = false
        this.colse_node.y = -560
        this.close_btn.interactable = true
        this.checkNoRole()

        let ver_info = he.local.getJson('ver_info')
        if (ver_info.banner_mistake) {
            this.colse_node.y = -560
        } else {
            this.colse_node.y = -350
            this.close_btn.interactable = true
        }
    },

    closeUI() {
        this.node.targetOff(this)
        he.ad.changeBanner(false, this.bottom)
    },

    checkNoRole() {
        let team = he.local.getJson('team', 'user')
        this.grp = [1001, 1002, 1003, 1004, 1005]
        this.numGrp = [1, 2, 3, 4, 5]
        for (let i = 0; i < team.length; i++) {
            this.grpDel(team[i].id)
            this.numGrpDel(team[i].order)
        }
        if (this.grp.length != 0) {
            this.newRole = {
                id: this.grp[this.randomNum(0, this.grp.length - 1)],
                order: this.numGrp[this.randomNum(0, this.numGrp.length - 1)],
                equips: [1, 0, 0, 0],
                hand: 0
            }
        }
        this.role_sp.spriteFrame = he.load.get('overPage', cc.SpriteAtlas, 'plist').getSpriteFrame(`pose_${this.newRole.id}`)
    },
    //看广告
    lookAdServer() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                he.ad.showVideo(() => {
                    let team = he.local.getJson('team', 'user')
                    team.push(this.newRole)
                    he.local.setJson('team', team, 'user')
                    gg.data.isTrail = true
                    gg.data.trailID = this.newRole.id
                    this.uiBase.closePage()
                    gg.data.goPass(gg.data.pass_id)
                    gg.uiManager.addPage(UI_Layer.Cover, {
                        name: "gogoMiniForce",
                        cover: true
                    })
                })
                break
            default:
                gg.data.goPass(gg.data.pass_id)
                this.uiBase.closePage()
                gg.uiManager.addPage(UI_Layer.Cover, {
                    name: "gogoMiniForce",
                    cover: true
                })

                break
        }
    },


    dumpServer() {
        this.uiBase.closePage()
        gg.data.goPass(gg.data.pass_id)
        gg.uiManager.addPage(UI_Layer.Cover, {
            name: "gogoMiniForce",
            cover: true
        })
    },

    grpDel(id) {
        for (let i = 0; i < this.grp.length; i++) {
            if (this.grp[i] == id) {
                this.grp.splice(i, 1)
                return
            }
        }
    },

    numGrpDel(num) {
        for (let i = 0; i < this.numGrp.length; i++) {
            if (this.numGrp[i] == num) {
                this.numGrp.splice(i, 1)
                return
            }
        }
    },

    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },

    rewardwxServer() {
        if (!this.isShowAd) {
            this.isShowAd = true
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    this.close_btn.interactable = false
                    let ver_info = he.local.getJson('ver_info')
                    if (ver_info.banner_mistake) {
                        this.scheduleOnce(() => {
                            this.bottom.name = WX_AD_CONFIG.bannerId[0]
                            he.ad.changeBanner(true, this.bottom)
                        }, 1.5)
                        this.scheduleOnce(() => {
                            this.colse_node.y = -350
                            // he.ad.changeBanner(false, this.bottom)
                            this.close_btn.interactable = true
                        }, 3)
                    } else {
                        this.dumpServer()
                    }
                    break
                default:
                    this.dumpServer()
                    break
            }
        } else {
            this.dumpServer()
        }
    },
});
